﻿<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"	exclude-result-prefixes="php">
	
    <xsl:include href="utility.xsl" />
    <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
    
    <xsl:template match="/response">
        <xsl:if test="@status='ok'">
            <xsl:apply-templates select="context" />
        </xsl:if>
        
        <xsl:if test="@status='error'">
        Error: <xsl:value-of select="message" />
        </xsl:if>
    </xsl:template>
	
    <xsl:template match="context">
        <div style="margin:10px 5px;">
            <h3 style="margin-bottom:5px;"><xsl:element name="a">
            <xsl:attribute name="rel">melative</xsl:attribute>
            <xsl:attribute name="href">http://melative.com/<xsl:value-of select="type" />/<xsl:value-of select="translate(alias,' ','+')" /></xsl:attribute><xsl:value-of select="alias" /></xsl:element>
            <xsl:call-template name="h3-small">
                <xsl:with-param name="first" select="property-list/first/value" />
                <xsl:with-param name="season" select="property-list/season/value" />
                <xsl:with-param name="type" select="property-list/type/value" />
            </xsl:call-template>
            </h3>

            <div class="img" style="float:left;"><xsl:element name="img">
                <xsl:attribute name="alt"><xsl:value-of select="alias" /></xsl:attribute>
                <xsl:attribute name="src"><xsl:value-of select="image_url" />?s=200</xsl:attribute>
            </xsl:element></div>
            
            <div class="cont" style="margin-left:216px;">
            <xsl:apply-templates select="property-list" />
            <xsl:apply-templates select="resource-list" />
            <xsl:apply-templates select="description" />
            </div>
            <hr style="clear:left;" />
        </div>

    </xsl:template>
    
    <!-- select the First and Season -->
    <xsl:template name="h3-small">
        <xsl:param name="first" />
        <xsl:param name="season" />
        <xsl:param name="type" />
        <xsl:if test="$first or $season or $type">
            <small>(<xsl:value-of select="php:functionString('first_season_type', $first, $season, $type)" />)</small>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="property-list">
        <strong>Details</strong><br />
        <xsl:for-each select="*">
            <xsl:choose>
                <xsl:when test="name() = 'genre'">Genre: <xsl:value-of select="./value" /><br /></xsl:when>
                <xsl:when test="name() = 'theme'">Theme: <xsl:value-of select="./value" /><br /></xsl:when>
                <xsl:when test="name() = 'status'">Status: <xsl:value-of select="./value" /><br /></xsl:when>
                <xsl:when test="name() = 'length'">Length: <xsl:value-of select="./value" /><br /></xsl:when>
                <xsl:when test="name() = 'duration'">Duration: <xsl:value-of select="./value" /><br /></xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="resource-list">
        <strong>Resources: </strong>
        <xsl:for-each select="*">
            <xsl:element name="a">
                <xsl:attribute name="href"><xsl:value-of select="url" /></xsl:attribute>
                <xsl:if test="title != ''"><xsl:attribute name="title"><xsl:value-of select="title" /></xsl:attribute></xsl:if>
                <xsl:element name="img">
                    <xsl:attribute name="alt">favicon</xsl:attribute>
                    <xsl:attribute name="src">http://images.melative.com/res/<xsl:value-of select="substring-before(substring-after(url,'//'),'/')" /></xsl:attribute>
                </xsl:element>
            </xsl:element>
        </xsl:for-each>
        <br /><br />
    </xsl:template>
    
    <xsl:template match="description">
        <strong>Description</strong><br />
        <xsl:value-of disable-output-escaping="yes" select="php:functionString('description',.)" />
    </xsl:template>
    
</xsl:stylesheet>
